package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.dao.ContractCoinMapper;
import cn.ztuo.bitrade.entity.ContractCoin;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Path;
import java.util.List;

@Service
public class ContractCoinService {

    @Autowired
    private ContractCoinMapper coinMapper;

    public ContractCoin findBySymbol(String symbol) {
        return coinMapper.findBySymbol(symbol);
    }
    public ContractCoin saveAndFlush(ContractCoin coin) {
        return coinMapper.saveAndFlush(coin);
    }

    public ContractCoin save(ContractCoin contractCoin) {
        return coinMapper.save(contractCoin);
    }
    public List<ContractCoin> save(List<ContractCoin> coins) {
        return coinMapper.save(coins);
    }

    public List<ContractCoin> getBaseSymbol() {
        //return coinMapper.findBaseSymbol();
        return coinMapper.findAll();
    }

    public List<ContractCoin> selectByAll() {
        return coinMapper.findAll();
    }

    public List<ContractCoin> findAllEnabled() {
        Specification<ContractCoin> spec = (root, criteriaQuery, criteriaBuilder) -> {
            Path<String> enable = root.get("enable");
            criteriaQuery.where(criteriaBuilder.equal(enable, 1));
            return null;
        };
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "sort");
        Sort sort = new Sort(order);
        return coinMapper.findAll(spec, sort);

    }
    public List<ContractCoin> findAllSymbol() {
        return coinMapper.findAll();

    }
    public Page<ContractCoin> findAllEnabledByPage(Pageable pageable) {
        Specification<ContractCoin> spec = (root, criteriaQuery, criteriaBuilder) -> {
            Path<String> enable = root.get("enable");
            criteriaQuery.where(criteriaBuilder.equal(enable, 1));
            return null;
        };
        return coinMapper.findAll(spec,pageable);

    }

    public ContractCoin findOne(Integer id) {
        return coinMapper.findById(id);
    }
    public Page<ContractCoin> findAll(Predicate predicate, Pageable pageable){
        return coinMapper.findAll(predicate,pageable);
    }

    public List<String> getCoinSymbol(String baseSymbol) {
        return coinMapper.findCoinSymbol(baseSymbol);
    }
}
